﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRevitBase.WinForm;
using YJKRevitView;
using YJKInspectModel.ModelValidation.ClearHeightCheck;

namespace YJKInspectModel.ModelValidation.Commands
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class ClearHeightPlanCmd : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			if (!(commandData.Application.ActiveUIDocument.Document.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((CmdViewType)3, commandData);
				if (result != Autodesk.Revit.UI.Result.Succeeded)
				{
					return result;
				}
			}
			if (ClearHeightPlanCmd._hWndRevit == null)
			{
                ClearHeightPlanCmd._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			Transaction transaction = new Transaction(document, "createfillplan");
			transaction.Start();
			try
			{
				Autodesk.Revit.DB.View view = null;
				Autodesk.Revit.DB.View activeView = document.ActiveView;
				int num = activeView.Name.IndexOf("净高分析图");
				if (-1 == num)
				{
					string value = "净高分析图-" + document.ActiveView.Name;
					foreach (Element element in new FilteredElementCollector(document).OfClass(typeof(Autodesk.Revit.DB.View)).ToElements())
					{
						Autodesk.Revit.DB.View view2 = element as Autodesk.Revit.DB.View;
						if (view2.Name.Equals(value))
						{
							view = view2;
							break;
						}
					}
					if (view == null)
					{
                        ElementId elementId = document.ActiveView.Duplicate((ViewDuplicateOption)2);
						view = (document.GetElement(elementId) as Autodesk.Revit.DB.View);
						view.Name = "净高分析图-" + document.ActiveView.Name;
					}
				}
				else
				{
					view = activeView;
				}
				transaction.Commit();
				activeUIDocument.ActiveView = view;
			}
			catch (Exception)
			{
				transaction.RollBack();
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (ClearHeightPlanCmd._hWndRevit == null)
			{
                ClearHeightPlanCmd._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			ClearHeightCheckFun clearHeightCheckFun = new ClearHeightCheckFun(application);
			new List<Element>();
			PlanTypeSelectForm planTypeSelectForm = new PlanTypeSelectForm(clearHeightCheckFun.getAllElements());
			while (planTypeSelectForm.ShowDialog() == DialogResult.OK)
			{
				if (planTypeSelectForm.getSelectedType() == 0)
				{
					new List<List<CurveLoop>>();
					ApplicationClearHeight.thisApp.DrawZoneManualNew(application, ClearHeightPlanCmd._hWndRevit, clearHeightCheckFun);
				}
				else
				{
					List<List<CurveLoop>> list = new List<List<CurveLoop>>();
					list = ApplicationClearHeight.thisApp.GetRoomLoops(activeUIDocument);
					if (list.Count < 1)
					{
						return Autodesk.Revit.UI.Result.Succeeded;
					}
					clearHeightCheckFun.setSelectType(false);
					clearHeightCheckFun.setUserSelectedArea(list);
					clearHeightCheckFun.CreateClearHeightPlan();
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

        public static WindowHandle _hWndRevit;
	}
}
